package com.blq.web.ServeTemplate.controller;

import java.util.List;
import java.util.Arrays;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.blq.common.annotation.RepeatSubmit;
import com.blq.common.annotation.Log;
import com.blq.common.core.controller.BaseController;
import com.blq.common.core.domain.PageQuery;
import com.blq.common.core.domain.R;
import com.blq.common.core.validate.AddGroup;
import com.blq.common.core.validate.EditGroup;
import com.blq.common.enums.BusinessType;
import com.blq.common.utils.poi.ExcelUtil;
import com.blq.web.ServeTemplate.domain.vo.ServeTemplateVo;
import com.blq.web.ServeTemplate.domain.bo.ServeTemplateBo;
import com.blq.web.ServeTemplate.service.IServeTemplateService;
import com.blq.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiOperation;

/**
 * 业务模板Controller
 *
 * @author Blq
 * @date 2022-11-07
 */
@Validated
@Api(value = "业务模板控制器", tags = {"业务模板管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/web/ServeTemplate")
public class ServeTemplateController extends BaseController {

    private final IServeTemplateService iServeTemplateService;

    /**
     * 查询业务模板列表
     */
    @ApiOperation("查询业务模板列表")
    @SaCheckPermission("web:ServeTemplate:list")
    @GetMapping("/list")
    public TableDataInfo<ServeTemplateVo> list(ServeTemplateBo bo, PageQuery pageQuery) {
        return iServeTemplateService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出业务模板列表
     */
    @ApiOperation("导出业务模板列表")
    @SaCheckPermission("web:ServeTemplate:export")
    @Log(title = "业务模板", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ServeTemplateBo bo, HttpServletResponse response) {
        List<ServeTemplateVo> list = iServeTemplateService.queryList(bo);
        ExcelUtil.exportExcel(list, "业务模板", ServeTemplateVo.class, response);
    }

    /**
     * 获取业务模板详细信息
     */
    @ApiOperation("获取业务模板详细信息")
    @SaCheckPermission("web:ServeTemplate:query")
    @GetMapping("/{id}")
    public R<ServeTemplateVo> getInfo(@ApiParam("主键")
                                      @NotNull(message = "主键不能为空")
                                      @PathVariable("id") Long id) {
        return R.ok(iServeTemplateService.queryById(id));
    }

    /**
     * 新增业务模板
     */
    @ApiOperation("新增业务模板")
    @SaCheckPermission("web:ServeTemplate:add")
    @Log(title = "业务模板", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ServeTemplateBo bo) {
        return toAjax(iServeTemplateService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改业务模板
     */
    @ApiOperation("修改业务模板")
    @SaCheckPermission("web:ServeTemplate:edit")
    @Log(title = "业务模板", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ServeTemplateBo bo) {
        return toAjax(iServeTemplateService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除业务模板
     */
    @ApiOperation("删除业务模板")
    @SaCheckPermission("web:ServeTemplate:remove")
    @Log(title = "业务模板", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@ApiParam("主键串")
                          @NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iServeTemplateService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
